package seqlist.singleList;


class Test{
    public static void main(String[] args) {
        SingleList singleList = new SingleList();
        singleList.add(7);
        singleList.add(3);
        singleList.add(5);
        singleList.add(7);
        singleList.add(7);
        singleList.add(7);
//        singleList.add(2,4);
//        singleList.del(3);
//        singleList.delAllVal(7);
//        singleList.update(6,0);
//        System.out.println(singleList.search(5));
        System.out.println(singleList.toString());
    }
}
public class SingleList {
    private Node dummyHead = new Node();
    private int size;
    //头插法
    public void add(int val){
        dummyHead.next = new Node(val,dummyHead.next);
        size++;
    }
    //索引插入
    public void add(int index,int val){
        if (index < 0 || index > size){
            System.err.println(index+"不合法");
            return;
        }
        //1先找到待插入的节点的位置的前驱节点
        Node prev = new Node();
        prev.next = dummyHead.next;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
//        Node node = new Node(val);
//        node.next = prev.next;
//        prev.next = node;
        prev.next = new Node(val,prev.next);
        size++;
    }
    //链表的遍历
    public String toString(){
        Node temp = dummyHead.next;
        String res = "";
        while (temp != null){
            res += temp.val;
            res += "->";
            temp = temp.next;
        }
        res += "null";
        return res;
    }
    //删除
    public void del(int index){
        if (index > size || index < 0){
            System.err.println(index+"不合法");
            return;
        }
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        prev.next = prev.next.next;
    }
    //删除
    public void delAllVal(int val){
        Node prev = dummyHead;
        while (prev.next != null){
            if (prev.next.val == val){
                prev.next = prev.next.next;
            }else {
                prev = prev.next;
            }
        }
    }
    //修改
    public void update(int index ,int val){
        if (!isTrue(index)){
            System.err.println(index+"不合法");
            return;
        }
        Node node = dummyHead;
        for (int i = 0; i < index; i++) {
            node = node.next;
        }
        node.val = val;
    }
    //查询
    public int search(int index){
        if (!isTrue(index)){
            System.err.println(index+"不合法");
            return -1;
        }
        Node node = dummyHead;
        for (int i = 0; i < index; i++) {
            node = node.next;
        }
        return node.val;
    }
    //判定index的合法性
    public boolean isTrue(int index){
        if (index < 0 || index > size){
            return false;
        }
        return true;
    }


}

class Node{
    int val;
    Node next;

    public Node() {
    }

    public Node(int val) {
        this.val = val;
    }

    public Node(int val, Node next) {
        this(val);
        this.next = next;
    }
}
